<?php

namespace App\Http\Controllers;

use App\Activity;
use App\ClickLog;
use App\Order;
use App\SendLog;
use App\ShareLog;
use App\Siphonlog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AnalysisController extends Controller {
	use Search;

	public function index() {
		$business = $this->getBusiness();
		return view('analysis.index', compact('business'));
	}

	public function getNums(Request $request) {
		$nums = [
			'import' => 0,
			'smsSend' => 0,
			'smsClickNum' => 0,
			'smsClickUser' => 0,
			'shareNum' => 0,
			'shareUser' => 0,
			'joinNum' => 0,
			'joinSuccess' => 0,
		];
		$orderModel = new Order();
		$clickModel = new ClickLog();
		$shareModel = new ShareLog();
		$mapIds = $this->getSearchMapIds($request);
		if ($mapIds) {
			$where = 'map_id in (' . implode(',', $mapIds) . ')';
			$nums['import'] = $orderModel->whereRaw($where)->count();
			$nums['smsSend'] = $orderModel->whereRaw($where . ' and type = 1')->count();
			$clickList = $clickModel->whereRaw($where)->get();
			if ($clickList) {
				$nums['smsClickNum'] = count($clickList);
				$nums['smsClickUser'] = count(array_merge(array_unique(array_column($clickList->toArray(), 'six_id'))));
			}
			$shareList = $shareModel->whereRaw($where)->get();
			if ($shareList) {
				$nums['shareNum'] = count($shareList);
				$nums['shareUser'] = count(array_merge(array_unique(array_column($shareList->toArray(), 'six_id'))));
			}
			$nums['joinNum'] = (new Siphonlog)->whereRaw($where)->count();
			$nums['joinSuccess'] = (new SendLog)->whereRaw($where . ' and wx_id <> -1')->count();
		}
		return view('analysis.num', compact('nums'));
	}

	public function getCharts(Request $request) {
		$activity_id = $request->input('activity');
		$xData = $yData = [];
		if (!$activity_id) {
			return view('analysis.chart', compact('xData', 'yData'));
		}
		$activity = (new Activity)->find($activity_id);
		if (!$activity || !$activity->start) {
			return view('analysis.chart', compact('xData', 'yData'));
		}
		$mapIds = $this->getSearchMapIds($request);
		$type = $request->input('type');
		$time = $request->input('time');
		$where = 'map_id in (' . implode(',', $mapIds) . ')';
		$stamp = 'created_at';
		$count = "id";
		switch ($type) {
		case 'smsSend':
			$table = 'orders';
			$where .= " and type = 1";
			$stamp = 'send_time';
			break;
		case 'smsClick':
			$table = 'clicklogs';
			break;
		case 'smsUser':
			$table = 'clicklogs';
			$count = "distinct(six_id)";
			break;
		case 'shareNum':
			$table = 'sharelogs';
			break;
		case 'shareUser':
			$table = 'sharelogs';
			$count = "distinct(six_id)";
			break;
		case 'joinNum':
			$table = 'siphonlogs';
			$stamp = 'create_time';
			break;
		case 'joinSuccess':
			$table = 'sendlogs';
			$where .= ' and wx_id <> -1';
			$stamp = 'create_time';
			break;
		default:
			return view('analysis.chart', compact('xData', 'yData'));
			break;
		}
		if ($time == 7 && $activity->days > 7) {
			$start = $activity->start_time;
			$expore_time = strtotime($activity->start_time) + 24 * 60 * 60 * 7;
			$end = $expore_time > time() ? date('Y-m-d H:i:s', time()) : date('Y-m-d H:i:s', $expore_time);
			$where .= " and {$stamp} between \"{$start}\" and \"{$end}\"";
		}

		$list = DB::table($table)->whereRaw($where)->selectRaw('DATE_FORMAT(' . $stamp . ', "%Y-%m-%d") as time,count(' . $count . ') as num')->groupBy('time')->get();
		foreach ($list as $key => $value) {
			array_push($xData, $value->time);
			array_push($yData, $value->num);
		}

		return view('analysis.chart', compact('xData', 'yData'));
	}

}
